package cn.tedu.hotel.serve.service.impl;

import cn.tedu.hotel.serve.ex.ServiceException;
import cn.tedu.hotel.serve.mapper.HotelIndexBannerMapper;
import cn.tedu.hotel.serve.mapper.HotelMapper;
import cn.tedu.hotel.serve.mapper.HotelOrdersCountMapper;
import cn.tedu.hotel.serve.mapper.HotelScoreMapper;
import cn.tedu.hotel.serve.pojo.dto.HotelIndexBannerDTO;
import cn.tedu.hotel.serve.pojo.entity.HotelIndexBanner;
import cn.tedu.hotel.serve.pojo.vo.*;
import cn.tedu.hotel.serve.service.IHotelIndexService;
import cn.tedu.hotel.serve.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class HotelIndexImpl implements IHotelIndexService {

    @Autowired
    HotelIndexBannerMapper hotelIndexBannerMapper;

    @Autowired
    HotelOrdersCountMapper hotelOrdersCountMapper;

    @Autowired
    HotelMapper hotelMapper;

    @Autowired
    HotelScoreMapper hotelScoreMapper;

    @Override
    public void addHotelIndexBanner(HotelIndexBannerDTO hotelIndexBannerDTO) {
        log.debug("开始处理【后台添加轮播图路径：{}】的业务",hotelIndexBannerDTO);
        HotelIndexBanner hotelIndexBanner=new HotelIndexBanner();
        BeanUtils.copyProperties(hotelIndexBannerDTO,hotelIndexBanner);
        int rows = hotelIndexBannerMapper.addHotelIndexBanner(hotelIndexBanner);
        if (rows!=1){
            String message = "服务器繁忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public List<HotelIndexBannerVO> getAdminHotelIndexBannerList() {
        log.debug("开始处理【后台查询全部的轮播图】的业务");
        List<HotelIndexBannerVO> list = hotelIndexBannerMapper.getAdminHotelIndexBannerList();
        log.debug("开始处理【后台查询全部的轮播图,数据：{}】的业务",list);
        return list;
    }

    @Override
    public void deleteAdminHotelIndexBanner(Long id) {
        log.debug("开始处理【后台根据轮播图id：{}删除轮播图】的业务",id);
        HotelIndexBannerVO hotelIndexBannerVO =
                hotelIndexBannerMapper.getAdminHotelIndexBannerById(id);
        if (hotelIndexBannerVO==null){
            String message = "轮播图不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = hotelIndexBannerMapper.deleteAdminHotelIndexBanner(id);
        if (rows!=1){
            String message = "服务器繁忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateAdminHotelIndexBannerById(Long id, HotelIndexBannerDTO hotelIndexBannerDTO) {
        log.debug("开始处理【后台根据轮播图id：{}修改轮播图】的业务",id);
        HotelIndexBannerVO hotelIndexBannerVO = hotelIndexBannerMapper.getAdminHotelIndexBannerById(id);
        if (hotelIndexBannerVO==null){
            String message = "轮播图不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        HotelIndexBanner hotelIndexBanner=new HotelIndexBanner();
        BeanUtils.copyProperties(hotelIndexBannerDTO,hotelIndexBanner);
        hotelIndexBanner.setId(id);
        int rows = hotelIndexBannerMapper.updateAdminHotelIndexBannerById(hotelIndexBanner);
        if (rows!=1){
            String message = "服务器繁忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public List<HotelOrdersCountInfoVO> getHotelOrderCountList() {
        log.debug("开始处理【前台查询酒店排名】的业务");
        List<HotelOrdersCountVO> list = hotelOrdersCountMapper.getHotelOrderCountList();
        List<HotelOrdersCountInfoVO> newList=new ArrayList<>();
        for (HotelOrdersCountVO hotelOrdersCountVO : list) {
            HotelOrdersCountInfoVO hotelOrdersCountInfoVO=new HotelOrdersCountInfoVO();

            BeanUtils.copyProperties(hotelOrdersCountVO,hotelOrdersCountInfoVO);

            HotelStandardVO standardVO =
                    hotelMapper.getHotelById(hotelOrdersCountInfoVO.getHotelId());

            hotelOrdersCountInfoVO.setUrl(standardVO.getUrl());

            hotelOrdersCountInfoVO.setLocation(standardVO.getLocation());

            HotelLevelVO hotelLevelVO = hotelMapper.getByHotelLevelId(standardVO.getLevelId());

            hotelOrdersCountInfoVO.setType(hotelLevelVO.getType());

            hotelOrdersCountInfoVO.setAvgScore(getHotelAvgScore(hotelOrdersCountInfoVO.getHotelId()));

            newList.add(hotelOrdersCountInfoVO);
        }

        log.debug("开始处理【前台查询酒店排名】的业务,数据：{}",newList);

        return newList;
    }

    private BigDecimal getHotelAvgScore(Long hotelId) {
        log.debug("开始处理【前台展示酒店平均分数】的业务，参数：{}",hotelId);
        HotelStandardVO hotelStandardVO = hotelMapper.getHotelById(hotelId);
        if (hotelStandardVO==null){
            String message = "酒店不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        List<HotelScoreVO> list = hotelScoreMapper.getHotelScoreListByHotelId(hotelId);
        log.debug("返回的参数：{}",list);
        if (list.isEmpty()){
            BigDecimal avgScore=new BigDecimal(Double.toString(BASE_SCORE));
            avgScore=avgScore.setScale(1);
            return avgScore;
        }
        BigDecimal avgScore = hotelScoreMapper.getHotelAvgScoreByHotelId(hotelId);
        avgScore=avgScore.setScale(1,BigDecimal.ROUND_UP);
        return avgScore;
    }
}
